package com.zhouchb.test.设计模式.原型模式;

import javax.print.attribute.standard.MediaSize;
import java.io.*;
import java.util.Date;

/**
 * @Create 2023-06-26 21:22
 * @Description
 */
public class 浅克隆 {
    public static void main(String[] args) throws CloneNotSupportedException {
        Prototype prototype = new Prototype("林动",26);

        //克隆
        Prototype clone = prototype.clone();
        Prototype clone2 = prototype.clone();
        System.out.println(prototype);
        System.out.println(clone);
        System.out.println(clone2);
    }
}

class Prototype implements Cloneable , Serializable {
    public Prototype(String name , int age){
        this.name = name;
        this.age =age;
    }

    public String name;
    public int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    protected Prototype clone() throws CloneNotSupportedException {
        return (Prototype) super.clone();
    }

    @Override
    public String toString() {
        return "Prototype{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

class DepthProtoType implements Serializable,Cloneable{
    private  String skillName;
    public Prototype prototype ;

    public String getSkillName() {
        return skillName;
    }

    public void setSkillName(String skillName) {
        this.skillName = skillName;
    }

    public Prototype getPrototype() {
        return prototype;
    }

    public void setPrototype(Prototype prototype) {
        this.prototype = prototype;
    }

    @Override
    protected DepthProtoType clone() throws CloneNotSupportedException {
        DepthProtoType depthProtoType = (DepthProtoType) super.clone();
       depthProtoType.prototype = prototype.clone();
        return depthProtoType;
    }

    @Override
    public String toString() {
        return "DepthProtoType{" +
                "skillName='" + skillName + '\'' +
                ", prototype=" + prototype +
                '}';
    }

    public DepthProtoType deepClone(){
        //创建流对象
        ByteArrayOutputStream bos = null;
        ObjectOutputStream oos = null;
        ByteArrayInputStream bis = null;
        ObjectInputStream ois = null;
        try {
            //序列化
            bos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(bos);
            oos.writeObject(this);//当前对象以对象流的方式输出
            //反序列化
            bis = new ByteArrayInputStream(bos.toByteArray());
            ois = new ObjectInputStream(bis);
            DepthProtoType depthProtoType = (DepthProtoType) ois.readObject();
            return depthProtoType;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                bos.close();
                oos.close();
                bis.close();
                ois.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
return null;
    }
}

class test{
    public static void main(String[] args) throws Exception {
        DepthProtoType depthProtoType = new DepthProtoType();
        depthProtoType.setSkillName("武动乾坤");
        depthProtoType.prototype = new Prototype("林动",26);

/*        DepthProtoType clone = depthProtoType.clone();
        System.out.println(depthProtoType.prototype.hashCode());
        System.out.println(clone.prototype.hashCode());*/
        DepthProtoType clone = depthProtoType.deepClone();
        System.out.println(depthProtoType.prototype.hashCode());
        System.out.println(clone.prototype.hashCode());
    }
}